/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.edu.ifrs.poa.util;

import java.io.IOException;
import java.util.HashMap;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

/**
 *
 * @author karenborges
 */
public class MailSender {

    private String username;
    private String password;
    private String host;
    private String porta;

    public MailSender(String username, String password, String host, String porta) {
        this.username = username;
        this.password = password;
        this.host = host;
        this.porta = porta;
    }

    /**
     * Send an Email via Gmail SMTP server using SSL connection.
     * @param assunto
     * @param mensagem
     * @throws MessagingException 
     */
    public void sendMail(String assunto, String mensagem) throws MessagingException, IOException {
        Properties props = new Properties();
        props.put("mail.smtp.host", host);
        props.put("mail.smtp.socketFactory.port", porta);
        props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.port", porta);

        Session session = Session.getDefaultInstance(props,
                new javax.mail.Authenticator() {

                    protected PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication(username, password);
                    }
                });

        Message message = new MimeMessage(session);
        message.setFrom(new InternetAddress(username));
        message.setRecipients(Message.RecipientType.TO,
                InternetAddress.parse("karen.borges@poa.ifrs.edu.br"));
        message.setSubject(assunto);
        message.setText(mensagem);

        Transport.send(message);

    }
//    public static void main(String[] args) {
//        try {
//                MailSender.sendMail("mensagem de teste2", "o assunto é teste de novo");
//            
//            System.out.println("email enviado com sucesso !");
//        } catch (MessagingException ex) {
//            System.out.println(ex);
//        } catch (IOException ex) {
//                Logger.getLogger(MailSender.class.getName()).log(Level.SEVERE, null, ex);
//            }
//    }
}
